package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author: 李旭
 * @create: 2021-08-30 09:28
 * <p>
 * 商品详情微服务   数据汇总
 * <p>
 * Swagger2 规则 ： 网站  /api/.*    通过Swagger2进行测试
 **/
@RestController
@RequestMapping("/api/item")
public class ItemApiController {

    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private ListFeignClient listFeignClient;

    //查询商品详情页面需要的所有数据
    @GetMapping("/getItem/{skuId}")
    public Map getItem(@PathVariable Long skuId) {
        Map result = new HashMap();

        //1:库存信息 合并 库存图片  根据库存ID查询库存信息
        CompletableFuture<SkuInfo> skuInfoCompletableFuture =
                CompletableFuture.supplyAsync(() -> {
                    SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
                    result.put("skuInfo", skuInfo);
                    return skuInfo;
                }, threadPoolExecutor);
        //2:一二三级分类  入参：三级分类ID
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView baseCategoryView = productFeignClient.
                    getBaseCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView", baseCategoryView);
        }, threadPoolExecutor);
        //3:单独查询商品价格  价格不允许缓存  要求实时更新
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            BigDecimal price = productFeignClient.getPrice(skuId);
            result.put("price", price);
        }, threadPoolExecutor);

        //4:销售属性及属性值（选中项）标红
        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrListCheckBySku = productFeignClient.
                    getSpuSaleAttrListCheckBySku(skuInfo.getId(), skuInfo.getSpuId());
            result.put("spuSaleAttrList", spuSaleAttrListCheckBySku);
        }, threadPoolExecutor);

        //5:点击销售属性值组合 对应的库存ID
        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            result.put("valuesSkuJson", JSONObject.toJSONString(skuValueIdsMap));
        }, threadPoolExecutor);

        //6:远程调用搜索微服务 增加热度评分  1
        CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId,1);//
        },threadPoolExecutor);

        //主线程要不要等待子线程  必须等待
        CompletableFuture.allOf(skuInfoCompletableFuture,categoryViewCompletableFuture
        ,priceCompletableFuture,spuSaleAttrListCompletableFuture,
                valuesSkuJsonCompletableFuture).join();
        return result;
    }


}
